package com.fjwt.gz.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjwt.gz.core.constants.Constants;
import com.fjwt.gz.core.exception.BizException;
import com.fjwt.gz.core.model.ApiRes;
import com.fjwt.gz.db.dto.ActivityHelpDTO;
import com.fjwt.gz.db.entity.*;
import com.fjwt.gz.db.vo.ActivityAwardVO;
import com.fjwt.gz.db.vo.ActivityHelpVO;
import com.fjwt.gz.service.mapper.ActivityHelpMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 用户发起活动助力表 服务实现类
 * </p>
 *
 * @author shiyonghao
 * @since 2025-04-09
 */
@Slf4j
@Service
public class ActivityHelpService extends ServiceImpl<ActivityHelpMapper, ActivityHelpEntity> {

    @Autowired
    private ActivityHelpMapper activityHelpMapper;
    @Autowired
    private ActivityShareService activityShareService;
    @Autowired
    private ActivityAwardService activityAwardService;
    @Autowired
    private AwardGoodsService awardGoodsService;
    @Autowired
    private AwardScoreService awardScoreService;
    @Autowired
    private AwardCouponService awardCouponService;
    @Autowired
    private AwardVirtualService awardVirtualService;
    @Autowired
    private AwardVirtualInsideService awardVirtualInsideService;

    /**
     * 发起人详情
     * @param dto
     * @return
     */
    public ActivityHelpVO details(ActivityHelpDTO dto) {

        ActivityHelpEntity activityHelp = BeanUtil.copyProperties(dto, ActivityHelpEntity.class);
        // 活动ID和用户ID不能为空
        if (ObjectUtils.isEmpty(activityHelp.getActivityId()) || ObjectUtils.isEmpty(activityHelp.getFansId())) {
            throw new BizException("请求参数异常，请检查！");
        }
        // 查询活动配置
        List<ActivityShareEntity> activityShareList = activityShareService.list(ActivityShareEntity.gw().eq(ActivityShareEntity::getActivityId, activityHelp.getActivityId()));
        if (ObjectUtils.isEmpty(activityShareList)) {
            throw new BizException("活动配置有误");
        }
        ActivityShareEntity activityShareEntity = activityShareList.get(0);

        // 查询用户参与记录
        LambdaQueryWrapper<ActivityHelpEntity> fActivityHelpGW = ActivityHelpEntity.gw();
        fActivityHelpGW.eq(ActivityHelpEntity::getActivityId,activityHelp.getActivityId());
        fActivityHelpGW.eq(ActivityHelpEntity::getFansId,activityHelp.getFansId());
        fActivityHelpGW.orderByDesc(ActivityHelpEntity::getActivityHelpId);
        if (activityShareEntity.getShareMaxNum() > 1) {
            if (activityShareEntity.getSharePeriodType() > 0) {
                DateTime date = DateUtil.date();
                if (activityShareEntity.getSharePeriodType() == 1) {
                    fActivityHelpGW.between(ActivityHelpEntity::getCreatedAt, DateUtil.beginOfDay(date), DateUtil.endOfDay(date));
                }else if (activityShareEntity.getSharePeriodType() == 2) {
                    fActivityHelpGW.between(ActivityHelpEntity::getCreatedAt, DateUtil.beginOfWeek(date), DateUtil.endOfWeek(date));
                }else if (activityShareEntity.getSharePeriodType() == 3) {
                    fActivityHelpGW.between(ActivityHelpEntity::getCreatedAt, DateUtil.beginOfMonth(date), DateUtil.endOfMonth(date));
                }
            }
        }

        List<ActivityHelpEntity> fActivityHelpList = activityHelpMapper.selectList(fActivityHelpGW);
        if(ObjectUtils.isEmpty(fActivityHelpList)){
            return null;
        }
        ActivityHelpEntity fActivityHelp = fActivityHelpList.get(0);

        // 组装返回数据
        ActivityHelpVO fActivityHelpVO = new ActivityHelpVO();
        BeanUtils.copyProperties(fActivityHelp, fActivityHelpVO);
        fActivityHelpVO.setExchangeType(activityShareEntity.getExchangeType());
        // 查询奖品
        LambdaQueryWrapper<ActivityAwardEntity> activityAwardGW = ActivityAwardEntity.gw();
        activityAwardGW.eq(ActivityAwardEntity::getActivityId,fActivityHelp.getActivityId());
        List<ActivityAwardEntity> activityAwardList = activityAwardService.list(activityAwardGW);

        fActivityHelpVO.setActivityAwardList(getAwardList(activityAwardList));

        return fActivityHelpVO;
    }

    /**
     * 获取活动奖品
     */
    public List<ActivityAwardVO> getAwardList(List<ActivityAwardEntity> activityAwardList) {
        List<ActivityAwardVO> list = new ArrayList<>();
        for (ActivityAwardEntity entity : activityAwardList) {
            Long awardId = entity.getAwardId();

            ActivityAwardVO activityAwardVO = new ActivityAwardVO();
            activityAwardVO.setActivityAwardId(entity.getActivityAwardId());
            activityAwardVO.setAwardId(awardId);
            activityAwardVO.setAwardType(entity.getAwardType());
            activityAwardVO.setExchangeShareNum(entity.getExchangeShareNum());

            switch (entity.getAwardType()) {
                case Constants.AWARD.COUPON:
                    AwardCouponEntity awardCouponEntity = awardCouponService.getById(awardId);
                    activityAwardVO.setAwardName(awardCouponEntity.getCouponName());
                    activityAwardVO.setAwardPrice(awardCouponEntity.getCouponPrice());
                    activityAwardVO.setPayAmount(awardCouponEntity.getPayAmount());
                    activityAwardVO.setRemark(awardCouponEntity.getRemark());
                    break;
                case Constants.AWARD.GOODS:
                    AwardGoodsEntity awardGoodsEntity = awardGoodsService.getById(awardId);
                    activityAwardVO.setAwardName(awardGoodsEntity.getGoodsName());
                    activityAwardVO.setAwardPrice(awardGoodsEntity.getGoodsPrice());
                    activityAwardVO.setAwardImg(awardGoodsEntity.getGoodsImgBanner());
                    activityAwardVO.setRemark(awardGoodsEntity.getRemark());
                    break;
                case Constants.AWARD.SCORE:
                    AwardScoreEntity awardScoreEntity = awardScoreService.getById(awardId);
                    activityAwardVO.setAwardName(awardScoreEntity.getScoreName());
                    activityAwardVO.setAwardPrice(awardScoreEntity.getScoreValue());
                    activityAwardVO.setRemark(awardScoreEntity.getRemark());
                    break;
                case Constants.AWARD.VIRTUAL_COUPON:
                    AwardVirtualEntity awardVirtualEntity = awardVirtualService.getById(awardId);
                    AwardVirtualInsideEntity awardVirtualInsideEntity = awardVirtualInsideService.getById(awardVirtualEntity.getVirtualInsideId());
                    activityAwardVO.setAwardName(awardVirtualInsideEntity.getGoodsName());
                    activityAwardVO.setAwardPrice(awardVirtualInsideEntity.getGoodsPrice());
                    activityAwardVO.setAwardImg(awardVirtualInsideEntity.getGoodsImgListSmall());
                    activityAwardVO.setRemark(awardVirtualEntity.getRemark());
                    break;
            }
            list.add(activityAwardVO);
        }

        return list;
    }

    /**
     * 新增发起活动助力
     * @param dto
     */
    public void saveActivityHelp(ActivityHelpDTO dto) {
        ActivityHelpEntity activityHelp = BeanUtil.copyProperties(dto, ActivityHelpEntity.class);
        activityHelp.setSuccessStandardNum(0);
        activityHelp.setTotalHelpNum(0);
        activityHelp.setSurplusSuccessNum(0);
        activityHelp.setCreatedAt(new Date());
        activityHelpMapper.insert(activityHelp);
    }
}
